﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using PmSoft;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PmSoft
{
    public static class HttpContextAccessorExtensions
    {
        /// <summary>
        /// 客户端是否认证
        /// </summary>
        /// <param name="contextAccessor"></param>
        /// <returns></returns>
        public static bool IsAuthenticated(this IHttpContextAccessor contextAccessor)
        {
            if (contextAccessor == null
                || contextAccessor.HttpContext == null
                || contextAccessor.HttpContext.User == null
                || contextAccessor.HttpContext.User.Identity == null)
                return false;
            return contextAccessor.HttpContext.User.Identity.IsAuthenticated;
        }

        /// <summary>
        /// 获取客户端请求的URL
        /// </summary>
        /// <param name="contextAccessor"></param>
        /// <returns></returns>
        public static string GetRequestUrl(this IHttpContextAccessor contextAccessor)
        {
            if (contextAccessor == null || contextAccessor.HttpContext == null)
                return string.Empty;
            return contextAccessor.HttpContext.Request.Path;
        }

        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <param name="contextAccessor"></param>
        /// <returns></returns>
        public static string GetRemoteIpAddress(this IHttpContextAccessor contextAccessor)
        {
            if (contextAccessor == null || contextAccessor.HttpContext == null)
                return string.Empty;

            var userHostAddress = contextAccessor.HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();

            if (string.IsNullOrEmpty(userHostAddress))
                userHostAddress = contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();

            IPAddress address;
            if (!IPAddress.TryParse(userHostAddress, out address))
                return userHostAddress;

            return address.MapToIPv4().ToString();
        }

        public static IEnumerable<T> GetServices<T>(this IHttpContextAccessor contextAccessor)
        {
            if (contextAccessor == null || contextAccessor.HttpContext == null)
                return new List<T>();

            return contextAccessor.HttpContext.RequestServices.GetServices<T>();
        }

        public static T? GetService<T>(this IHttpContextAccessor contextAccessor)
        {
            if (contextAccessor == null || contextAccessor.HttpContext == null)
                return default(T);

            return contextAccessor.HttpContext.RequestServices.GetService<T>();
        }

        public static object? GetService(this IHttpContextAccessor contextAccessor, Type serviceType)
        {
            if (contextAccessor == null || contextAccessor.HttpContext == null)
                return null;
            return contextAccessor.HttpContext.RequestServices.GetService(serviceType);
        }
    }
}
